/**
 * 尺寸相关CSS规则模块
 * @version 1.0.0
 */

import { utils } from '../../../utils/utils.js';

// 处理尺寸相关的CSS规则
export function processSizeRules(className, options = {}) {
    const { platform = 'H5', h5Unit = 'rem', remBase = 16 } = options;
    
    // 宽度高度
    const sizeMatch = className.match(/^(w|h|min-w|max-w|min-h|max-h)-(.+)$/);
    if (sizeMatch) {
        const [, prop, value] = sizeMatch;
        const propertyMap = {
            'w': 'width',
            'h': 'height',
            'min-w': 'min-width',
            'max-w': 'max-width',
            'min-h': 'min-height',
            'max-h': 'max-height'
        };
        
        const property = propertyMap[prop];
        let cssValue;
        
        // 处理带单位的值 (如: 80rpx, 50%, 100vh, 2rem 等)
        const unitMatch = value.match(/^(\d+(?:\.\d+)?)(rpx|px|rem|em|vh|vw|vmin|vmax|%|pt|pc|in|mm|cm)$/);
        if (unitMatch) {
            const [, numValue, unit] = unitMatch;
            
            // uni-app x 只支持 number|pixel|percent|auto
            if (platform === 'APP-PLUS' || platform === 'APP-ANDROID' || platform === 'APP-IOS') {
                if (unit === 'rem') {
                    cssValue = `${numValue * remBase}px`;
                } else if (unit === 'em') {
                    cssValue = `${numValue * 16}px`;
                } else if (unit === 'vw') {
                    // vw转换为百分比（假设100vw = 100%）
                    cssValue = `${numValue}%`;
                } else if (unit === 'vh') {
                    // vh转换为百分比
                    cssValue = `${numValue}%`;
                } else if (unit === 'vmin' || unit === 'vmax') {
                    cssValue = `${numValue}%`;
                } else if (unit === '%') {
                    cssValue = `${numValue}%`;
                } else if (unit === 'rpx') {
                    cssValue = `${numValue}rpx`;
                } else if (unit === 'px') {
                    cssValue = `${numValue}px`;
                } else {
                    // 其他单位转换为px
                    cssValue = `${numValue * 16}px`;
                }
            } else {
                cssValue = `${numValue}${unit}`;
            }
        }
        // 处理分数值 (如: 1/2, 1/3, 2/3, 1/4, 3/4 等)
        else if (value.includes('/')) {
            const [numerator, denominator] = value.split('/').map(Number);
            if (numerator && denominator && numerator > 0 && denominator > 0) {
                const percentage = (numerator / denominator * 100).toFixed(6);
                cssValue = `${parseFloat(percentage)}%`;
            } else {
                cssValue = utils.convertUnit(value, platform, { h5Unit, remBase });
            }
        }
        // 处理预设值
        else if (value === 'full') cssValue = '100%';
        else if (value === 'screen') {
            // uni-app x 不支持vw，转换为100%
            cssValue = (platform === 'APP-PLUS' || platform === 'APP-ANDROID' || platform === 'APP-IOS') ? '100%' : '100vw';
        }
        else if (value === 'auto') cssValue = 'auto';
        else if (value === 'fit') {
            // uni-app x 不支持fit-content，使用auto
            cssValue = (platform === 'APP-PLUS' || platform === 'APP-ANDROID' || platform === 'APP-IOS') ? 'auto' : 'fit-content';
        }
        else if (value === 'min') {
            // uni-app x 不支持min-content，使用auto
            cssValue = (platform === 'APP-PLUS' || platform === 'APP-ANDROID' || platform === 'APP-IOS') ? 'auto' : 'min-content';
        }
        else if (value === 'max') {
            // uni-app x 不支持max-content，使用auto
            cssValue = (platform === 'APP-PLUS' || platform === 'APP-ANDROID' || platform === 'APP-IOS') ? 'auto' : 'max-content';
        }
        else if (value === 'half') {
            // 自定义值：half -> 50%
            cssValue = '50%';
        }
        else if (value === 'quarter') {
            // 自定义值：quarter -> 25%
            cssValue = '25%';
        }
        // 处理纯数字值（使用平台单位转换）
        else cssValue = utils.convertUnit(value, platform, { h5Unit, remBase, unitType: 'sizing' });
        
        return { [property]: cssValue };
    }
    
    return null;
} 